package cs2340.g21.wms;

import java.util.ArrayList;
import android.os.Bundle;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class AdminControlsActivity extends WMSActivity {
    
    private static final String LOG_TAG = "debugger";
    
    private Coordinator coordinator;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        coordinator = new Coordinator();
        if(getIntent().getExtras() != null){
            coordinator = (Coordinator) getIntent().getExtras().getSerializable("cs2340.g21.wms.coordinator");
        }
        
        setContentView(R.layout.activity_admin_controls);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_admin_controls, menu);
        return true;
    }
    
    public void signOut(View view) {
        Intent intent = new Intent();
        coordinator.resetUser();
        intent.putExtra("cs2340.g21.wms.coordinator", coordinator);
        intent.setClass(AdminControlsActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }
    
    public void viewNotes(View view){
        //Log.d(LOG_TAG,"hey this gets called");
        Intent intent = new Intent();
        intent.putExtra("cs2340.g21.wms.coordinator", coordinator);
        intent.putExtra("notes", (ArrayList<Note>)coordinator.getAdminNotes());
        intent.setClass(AdminControlsActivity.this, NoteListActivity.class);
        startActivity(intent);
        finish();
    }
    
    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        coordinator.resetUser();
        intent.putExtra("cs2340.g21.wms.coordinator", coordinator);
        intent.setClass(AdminControlsActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    /**
     * opens a dialog where an admin can enter the email of an account to unlock
     * @param view
     */
    public void unlockAccount(View view){
        AlertDialog.Builder unlockBuild = new AlertDialog.Builder(this);
        unlockBuild.setTitle("Unlock account");
        final EditText input = new EditText(this);
        unlockBuild.setView(input);
        
        unlockBuild.setPositiveButton(R.string.prompt_confirm, new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String email = input.getText().toString();
                if(!coordinator.checkEmailAvailable(email)){
                    User u = coordinator.getAccount(coordinator.getAccountId(email));
                    u.unlock();
                }else{
                    input.setError(getString(R.string.error_invalid_email));
                }
            }
        });
        
        unlockBuild.setNegativeButton(R.string.action_cancel, new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                
            }
            
        });
        
        unlockBuild.show();
    }
    
    /**
     * opens a dialog where an admin can enter the email of an account to remove
     * @param view
     */
    public void removeAccount(View view){
        AlertDialog.Builder removeBuild = new AlertDialog.Builder(this);
        removeBuild.setTitle("Remove account");
        final EditText input = new EditText(this);
        removeBuild.setView(input);
        
        removeBuild.setPositiveButton(R.string.prompt_confirm, new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String email = input.getText().toString();
                if(!coordinator.checkEmailAvailable(email)){
                    coordinator.removeAccount(email);
                }else{
                    input.setError(getString(R.string.error_invalid_email));
                }
            }
        });
        
        removeBuild.setNegativeButton(R.string.action_cancel, new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                
            }
            
        });
        
        removeBuild.show();
    }
    
    /**
     * opens a dialog where an admin can enter login details for a new admin account
     * @param view
     */
    public void addAdmin(View view){
        AlertDialog.Builder addBuild = new AlertDialog.Builder(this);
        LayoutInflater inflater = this.getLayoutInflater();
        addBuild.setTitle("Add admin");

        
        final View v = inflater.inflate(R.layout.dialog_add_admin, null);
        addBuild.setView(v);
        
        addBuild.setPositiveButton(R.string.prompt_confirm, new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                EditText email = (EditText)v.findViewById(R.id.newAdminUser);
                EditText password = (EditText)v.findViewById(R.id.newAdminPass);
                String aEmail = email.getText().toString();
                String aPass = password.getText().toString();
                //Log.d(LOG_TAG, "value "+aPass);
                //Log.d(LOG_TAG, "value "+aEmail);
                if(coordinator.checkEmailAvailable(aEmail) && aEmail.matches("[a-z0-9]+@[a-z0-9]+.(com|gov|edu|net|ru)") &&
                    aPass.length() > 3){
                    //Log.d(LOG_TAG, "password "+aPass);
                    coordinator.addAdmin(aEmail, aPass);
                }else{
                    email.setError(getString(R.string.error_invalid_email));
                }
            }
        });
        
        addBuild.setNegativeButton(R.string.action_cancel, new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                
            }
            
        });
        addBuild.show();
    }
    
    protected void onPause(){
        super.onPause(coordinator);
    }
}
